package com.blyat.xsoft.kernel.component.mybatis.interceptor;

import com.blyat.xsoft.kernel.component.mybatis.dialect.Dialect;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.Interceptor;

/**
 * @author syh
 * @date 2019/11/8
 **/
public abstract class AbstractInterceptor implements Interceptor {

    protected Dialect dialect;

    public void setDialect(Dialect dialect) {
        this.dialect = dialect;
    }

    protected MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.getKeyGenerator());
        if (ms.getKeyProperties() != null) {
            String[] var4 = ms.getKeyProperties();
            int var5 = var4.length;

            for (int var6 = 0; var6 < var5; ++var6) {
                String keyProperty = var4[var6];
                builder.keyProperty(keyProperty);
            }
        }

        builder.timeout(ms.getTimeout());
        builder.parameterMap(ms.getParameterMap());
        builder.resultMaps(ms.getResultMaps());
        builder.cache(ms.getCache());
        builder.useCache(ms.isUseCache());
        return builder.build();
    }
}
